package com.example.hospitallogin.modules.register.bff;

import com.example.hospitallogin.modules.login.dao.entity.HospitalMessageEntity;
import com.example.hospitallogin.modules.login.service.HospitalManegerService;
import com.example.hospitallogin.modules.register.dao.entity.HospitalRegisterEntity;
import com.example.hospitallogin.modules.register.dao.entity.LoginEntity;
import com.example.hospitallogin.modules.register.dao.entity.UserMassageEntity;
import com.example.hospitallogin.modules.register.dto.RegisterHospitalReq;
import com.example.hospitallogin.modules.register.dto.RegisterUserReq;
import com.example.hospitallogin.modules.register.service.LoginService;
import com.example.hospitallogin.modules.register.service.RegisterHospitalService;
import com.example.hospitallogin.modules.register.service.RegisterUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author qiancheng@cqxyy.net
 * @date 2022/9/15
 */
@Service
public class RegisterImpl {

    @Autowired
    private RegisterHospitalService registerHospitalService;

    @Autowired
    private RegisterUserService registerUserService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private HospitalManegerService hospitalManegerService;

    @Transactional
    public String registerHospital(RegisterHospitalReq req){
        List<HospitalMessageEntity> tmp = new ArrayList<>();
        Example example = new Example(HospitalMessageEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("idCard",req.getIdCard()).andEqualTo("hospitalName",req.getHospitalName());
        tmp = hospitalManegerService.selectByExample(example);
        if (CollectionUtils.isNotEmpty(tmp)){
            return "此医院已经注册";
        }
        List<HospitalRegisterEntity> list = new ArrayList<>();
        example = new Example(HospitalRegisterEntity.class);
        criteria = example.createCriteria();
        criteria.andEqualTo("idCard",req.getIdCard());
        list = registerHospitalService.selectByExample(example);
        if(CollectionUtils.isNotEmpty(list)){
            return "你已经申请注册，请勿重复申请";
        }
        HospitalRegisterEntity hospitalRegisterEntity = new HospitalRegisterEntity();
        BeanUtils.copyProperties(req,hospitalRegisterEntity);
        hospitalRegisterEntity.setUuid(UUID.randomUUID().toString());
        registerHospitalService.insertSelective(hospitalRegisterEntity);
        return "";
    }

    public String registerUser(RegisterUserReq req) {
        String phone = req.getUserPhone();
        Example example = new Example(LoginEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("loginPhone",phone);
        List<LoginEntity> list = loginService.selectByExample(example);
        if(!list.isEmpty()){
            return "此手机号已被注册";
        }
        //存入用户信息
        UserMassageEntity userMassageEntity = new UserMassageEntity();
        BeanUtils.copyProperties(req,userMassageEntity);
        String uuid = UUID.randomUUID().toString();
        userMassageEntity.setUuid(uuid);
        registerUserService.insertSelective(userMassageEntity);
        //存入登录接口
        LoginEntity loginEntity = new LoginEntity();
        loginEntity.setUuid(uuid);
        loginEntity.setLoginPhone(phone);
        loginEntity.setLoginPassword(req.getPassword());
        loginEntity.setType(1);
        loginService.insertSelective(loginEntity);
        return "注册成功";
    }
}
